//
//  OMConfigureViewController.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/4/24.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMConfigureViewController.h"
#import "KTElementTableCell.h"

@interface OMConfigureViewController ()
@property (nonatomic, strong) KTElementTableCell *agreementCell; //服务条款
@property (nonatomic, strong) KTElementTableCell *cacheClearCell;//清除缓存
@property (nonatomic, strong) KTElementTableCell *copyrightCell; //版权信息

@property (nonatomic, strong) NSArray *titles;
@end

@implementation OMConfigureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self configure];
}


/**
 初始化
 */
- (void)configure
{
    _titles = @[
                @[
                    @"服务条款",
                    @"清除缓存",],
                //                @[
                //                    @"版权信息",
                //                    ],
                ];

    UIView *footer = [UIView new];
    footer.backgroundColor = SM_whiteColor;
    [self.view addSubview:footer];
    [footer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.left.equalTo(@0);
        make.height.equalTo(@(SM_SIZE(130)));
    }];

    UIView *line = [UIView new];
    line.backgroundColor = SM_naviShadowColor;
    [footer addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(@0);
        make.height.equalTo(@(.5f));
    }];

    UIButton *logoutBtn = [UIButton new];
    [logoutBtn setBackgroundImage:[UIImage imageWithColor:SM_sangouColor] forState:UIControlStateNormal];
    [logoutBtn setTitle:@"退出帐号" forState:UIControlStateNormal];
    [logoutBtn setTitleColor:SM_blackColor forState:UIControlStateNormal];
    [logoutBtn.layer setCornerRadius:SM_SIZE(10)];
    [logoutBtn setClipsToBounds:YES];
    [logoutBtn.titleLabel setFont:SM_CFont(34)];
    [logoutBtn addTarget:self action:@selector(logoutAction:) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:logoutBtn];
    [logoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@(SM_SIZE(20)));
        make.right.bottom.equalTo(@(SM_SIZE(-20)));
    }];

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    footer.hidden = ![Storage isLogin];
}

#pragma mark - Actions

/**
 登出

 @param button 按钮实体
 */
- (void)logoutAction:(UIButton *)button
{
    KTSystemStorageSingleton.account = nil;
    KTSystemStorageSingleton.numOfCartItem = 0;
    [Storage setLogin:NO];
    //    KTSystemStorageSingleton.username       = nil;
    KTSystemStorageSingleton.userPassword   = @"";
    [self viewWillAppear:NO];
    NaviPopToRoot(true);
}

#pragma mark - UITableview delegate && datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _titles.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_titles[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KTElementTableCell *cell = [KTElementTableCell tableViewCellWithTableView:tableView];
    cell.elementLabel.text = _titles[indexPath.section][indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [KTElementTableCell cellHeight];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *title = _titles[indexPath.section][indexPath.row];
    if ([title isEqualToString:@"服务条款"]) {
        NaviPush(@"OMServiceRulesViewController", nil);
    }else if ([title isEqualToString:@"清除缓存"]) {
        [Storage removeImageDiskCache];
        [Storage removeImageMemoryCache];
        [Storage removeGetMethodCache];
        [OMHUD HUDInfo:@"已成功清除缓存"];
    }else if ([title isEqualToString:@"版权信息"]) {
        
    }
}
@end
